package top.xia17.plugins.fast.user.rest;

import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.model.PageData;
import top.xia17.plugins.fast.common.model.PageParam;
import top.xia17.plugins.fast.common.database.jpa.JpaUtil;
import top.xia17.plugins.fast.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.xia17.plugins.fast.user.dao.entity.Menu;
import top.xia17.plugins.fast.user.model.dto.MenuDto;
import top.xia17.plugins.fast.user.model.query.MenuQuery;
import top.xia17.plugins.fast.user.service.MenuService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 菜单rest
 *
 * @author xia17
 * @date 2022/1/17
 */
@RestController
@RequestMapping("menu/mgmt")
@RequiredArgsConstructor
public class MenuMgmtRest {

    private final MenuService menuService;


    /**
     * 新增菜单
     *
     * @param resource /
     * @return /
     */
    @PostMapping("create")
    public Back<Menu> create(@RequestBody @Valid Menu resource) {
        Menu menu = menuService.create(resource);
        return Back.ok(menu);
    }


    /**
     * 修改菜单
     *
     * @param resource /
     * @return /
     */
    @PostMapping("update")
    public Back<Menu> update(@RequestBody @Valid Menu resource) {
        Menu menu = menuService.update(resource);
        return Back.ok(menu);
    }

    /**
     * 删除菜单
     *
     * @param id 菜单id
     * @return /
     */
    @DeleteMapping
    public Back<String> delete(@NotBlank Long id) {
        VerifyUtil.isLongId(id, "参数异常");
        menuService.deleteById(id);
        return Back.ok();
    }


    /**
     * 菜单分页列表
     *
     * @param menuQuery 查询条件
     * @param pageParam 分页参数
     * @return /
     */
    @GetMapping
    public Back<PageData<Menu>> page(MenuQuery menuQuery, @Valid PageParam pageParam) {
        Page<Menu> page = menuService.findAll(menuQuery, pageParam.toPageable());
        return Back.ok(JpaUtil.page(page));
    }

    /**
     * 菜单列表
     *
     * @param query 查询条件
     * @return /
     */
    @GetMapping("list")
    public Back<List<Menu>> list(MenuQuery query) {
        List<Menu> list = menuService.findAll(query);
        return Back.ok(list);
    }

    /**
     * 获取菜单的树结构
     *
     * @return /
     */
    @GetMapping("get_menu_tree")
    public Back<List<MenuDto>> getDeptTree() {
        return Back.ok(menuService.getMenuTree());
    }


}
